
package com.dfl.ggbond.guidepage;

import android.animation.ArgbEvaluator;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.viewpager2.widget.ViewPager2;

import com.dfl.ggbond.R;
import com.dfl.ggbond.databinding.ActivityGuidePageBinding;

public class GuidePageActivity extends AppCompatActivity {

    private ActivityGuidePageBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityGuidePageBinding.inflate(getLayoutInflater());
        if (getSupportActionBar() != null) getSupportActionBar().hide();
        setContentView(binding.getRoot());


        final GuidePageAdapter guidePageAdapter = new GuidePageAdapter();

        // 二次进入标记
        SharedPreferences sp = getSharedPreferences("demo1", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("isFirst", false);
        editor.apply();

        // 绑定 ViewPager 与 circleindicator
        binding.viewpage.setAdapter(guidePageAdapter);
        binding.viewpage.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);

        binding.circleindicator.createIndicators(guidePageAdapter.getItemCount(), 1);
        binding.circleindicator.setViewPager(binding.viewpage);

        binding.main.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPage1));

        binding.viewpage.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            private final ArgbEvaluator evaluator = new ArgbEvaluator();

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                int pageCount = guidePageAdapter.getItemCount();
                if (position < 0 || position >= pageCount - 1) return;

                int[] colorArr = {
                        ContextCompat.getColor(GuidePageActivity.this, R.color.colorPage1),
                        ContextCompat.getColor(GuidePageActivity.this, R.color.colorPage2),
                        ContextCompat.getColor(GuidePageActivity.this, R.color.colorPage3)
                };

                int color = (int) evaluator.evaluate(positionOffset, colorArr[position], colorArr[position + 1]);
                binding.main.setBackgroundColor(color);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                int[] colorArr = {
                        ContextCompat.getColor(GuidePageActivity.this, R.color.colorPage1),
                        ContextCompat.getColor(GuidePageActivity.this, R.color.colorPage2),
                        ContextCompat.getColor(GuidePageActivity.this, R.color.colorPage3)
                };
                binding.main.setBackgroundColor(colorArr[position]);
            }
        });
    }
}